# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Newbyte <newbie13xd@gmail.com>
pkgname=mozjs91
pkgver=91.10.0
pkgrel=0
pkgdesc="Standalone Mozilla JavaScript engine (91 ESR)"
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey"
arch="x86_64 armv7 armhf aarch64 x86 ppc64le" # limited by rust and cargo
license="MPL-2.0"
depends_dev="
	$pkgname
	icu-dev
	libffi-dev
	nspr-dev
	readline-dev
	"
_llvmver=14
makedepends="
	$depends_dev
	autoconf2.13
	cargo
	clang-dev~$_llvmver
	linux-headers
	lld
	llvm$_llvmver
	perl
	python3
	rust
	sed
	zlib-dev
	"
checkdepends="icu-data-full"
subpackages="$pkgname-dev"
source="https://ftp.mozilla.org/pub/firefox/releases/${pkgver}esr/source/firefox-${pkgver}esr.source.tar.xz
	fix-i386-fdlibm.patch
	fix-musl-build.patch
	fix-rust-target.patch
	"
builddir="$srcdir"/firefox-$pkgver

prepare() {
	default_prepare
	mkdir "$srcdir/mozbuild"

	cat > .mozconfig <<- END
	ac_add_options --enable-application=js
	mk_add_options MOZ_OBJDIR='${PWD}'/obj
	ac_add_options --prefix=/usr
	ac_add_options --enable-release
	ac_add_options --enable-hardening
	ac_add_options --enable-optimize="$CFLAGS -O2"
	ac_add_options --enable-rust-simd
	ac_add_options --enable-linker=lld
	ac_add_options --disable-bootstrap
	ac_add_options --disable-debug
	ac_add_options --disable-debug-symbols
	ac_add_options --disable-jemalloc
	ac_add_options --disable-strip

	# System libraries
	ac_add_options --with-system-zlib
	ac_add_options --with-system-icu

	# Features
	ac_add_options --enable-readline
	ac_add_options --enable-shared-js
	ac_add_options --enable-tests
	ac_add_options --with-intl-api
	END
}

build() {
	export LDFLAGS="$LDFLAGS -Wl,-z,stack-size=1048576"

	# FF doesn't have SIMD available on armhf/v7
	case "$CARCH" in
	arm*)
		echo 'ac_add_options --disable-rust-simd' >> .mozconfig
		;;
	*)
		echo 'ac_add_options --enable-rust-simd' >> .mozconfig
		;;
	esac

	# disable debug on 32-bit (takes too much link memory)
	case "$CARCH" in
	arm*|x86)
		echo 'ac_add_options --disable-debug-symbols' >> .mozconfig
		echo 'ac_add_options --disable-debug' >> .mozconfig
		# hardcoded in the file
		sed -i 's|debug_info = "2"|debug_info = "0"|' \
			build/moz.configure/toolchain.configure
		;;
	esac

	export MACH_USE_SYSTEM_PYTHON=1
	export MOZBUILD_STATE_PATH="$srcdir/mozbuild"
	export MOZ_NOSPAM=1
	export PYTHON=/usr/bin/python3
	export RUST_TARGET="$CTARGET"
	export SHELL=/bin/ash

	./mach build
}

check() {
	obj/dist/bin/jsapi-tests \
		--format=none \
		--exclude-random \
		basic
}

package() {
	cd obj
	make DESTDIR="$pkgdir" install
	rm -f "$pkgdir"/usr/lib/*.ajs
}

sha512sums="
8344b829d7bd86250afdd4cb582e27ed5705b3ef48aec50b9a39abc17deba86c9fd721f4667f5c2155e3d7cd1d6e1f82ff8e218ced3a16a4e06bb414ee0690f8  firefox-91.10.0esr.source.tar.xz
2eeb9d3039104ba91854ba54283289fbc0f282c703ac7fa7107646dccd290fe80d09f2ebe0e4b0268349c755b6d82fcbe2c3788a20e5dc28ce24c7d545efde1d  fix-i386-fdlibm.patch
bc91c2fb15eb22acb8acc36d086fb18fbf6f202b4511d138769b5ecaaed4a673349c55f808270c762616fafa42e3b01e74dc0af1dcbeea1289e043926e2750c8  fix-musl-build.patch
0b222a36cf3fd446dfb7fd171d77c58c0f19bc4df8f61cb2d99f2dfa0998a24e4d11a92fb88a1144b1bdd8ace95a858dca0a5e9ad5a4ea37a13b2cb7c245758a  fix-rust-target.patch
"
